﻿using System;
using System.Linq;

namespace Array3DExtensions
{
    /// <summary>
    /// Represents the value in a 3d array
    /// Stanislav Radkov®  2012 :D 
    /// </summary>
    public class Cell3D<T> : Index3D, IComparable where T : IComparable<T>
    {
        public T Value { get; set; }

        #region Constructors

        public Cell3D(T[,,] array, Index3D cellIndex) : this(array[cellIndex.W, cellIndex.H, cellIndex.D], cellIndex)
        {
        }

        public Cell3D(T value, Index3D cellIndex) : base(cellIndex.W, cellIndex.H, cellIndex.D)
        {
            this.Value = value;
        }

        public Cell3D(T value, Int32 W, Int32 H, Int32 D) : base(W, H, D)
        {
            this.Value = value;
        }

        #endregion

        #region Public Methods

        public int CompareTo(object obj)
        {
            if (null == obj)
                return 1;

            Cell3D<T> otherCell = obj as Cell3D<T>;
            if (otherCell != null)
            {
                return this.Value.CompareTo(otherCell.Value);
            }
            else
            {
                throw new IndexOutOfRangeException("Object is not a Cell3D");
            }
        }

        public override int GetHashCode()
        {
            return this.Value.GetHashCode();
        }

        public override string ToString()
        {
            return base.ToString() + string.Format(" Value:{0}", this.Value);
        }
        #endregion
    }
}
